package ca.etsmtl.log730.projet2.connectors.clientserver;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;

public class LinkFactory {

	private Integer listenPort = 1234;

	public Integer getListenPort() {
		return listenPort;
	}

	public void setListenPort(Integer listenPort) {
		this.listenPort = listenPort;
	}
	
	private boolean connectToHost = false;
	private String connectionHostname = "";
	private Integer connectionPort = 1235;

	private String username = "bobo";
	
	public static int finNextAvailable(int beginport) {
		while(!available(beginport)) {
			beginport ++;
		}
		return beginport;
	}
	public static boolean available(int port) {

	    ServerSocket ss = null;
	    DatagramSocket ds = null;
	    try {
	        ss = new ServerSocket(port);
	        ss.setReuseAddress(true);
	        ds = new DatagramSocket(port);
	        ds.setReuseAddress(true);
	        return true;
	    } catch (IOException e) {
	    } finally {
	        if (ds != null) {
	            ds.close();
	        }

	        if (ss != null) {
	            try {
	                ss.close();
	            } catch (IOException e) {
	                /* should not be thrown */
	            }
	        }
	    }

	    return false;
	}

	public boolean isConnectToHost() {
		return connectToHost;
	}

	public void setConnectToHost(boolean connectToHost) {
		this.connectToHost = connectToHost;
	}

	public String getConnectionHostname() {
		return connectionHostname;
	}

	public void setConnectionHostname(String connectionHostname) {
		this.connectionHostname = connectionHostname;
	}

	public Integer getConnectionPort() {
		return connectionPort;
	}

	public void setConnectionPort(Integer connectionPort) {
		this.connectionPort = connectionPort;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
}
